﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jemgine.Engine.Scripts
{
    [Category("System"), Node]
    public class Destroy : Data.ScriptNode
    {
        public Destroy()
        {
            AddInput("Fire", null);
            AddReference("Entity", typeof(Data.EntityRecord));
            AddOutput("Relay", null);
        }

        public override void FireTerminal(int InputID, Data.ScriptPayload Payload)
        {
            var Target = ReferenceTerminals[0].GetTarget(Payload);
            if (Target != null && Target is Data.EntityRecord)
            {
                var Template = (Target as Data.EntityRecord).GetComponent<TemplatedEntityData>();
                if (Template != null)
                    Template.Creator.Recycle(Target as Data.EntityRecord);
                else
                    Sim.DestroyEntity(Target as Data.EntityRecord);
            }

            OutputTerminals[0].Fire(Payload);
        }
    }

    [Category("System"), Component]
    public class DestroySelf : EntityComponent
    {
        public DestroySelf()
        {
            AddInput("Fire", null);
            AddOutput("Relay", null);
        }

        public override void FireTerminal(int InputID, Data.ScriptPayload Payload)
        {
            var Template = Me.GetComponent<TemplatedEntityData>();
            if (Template != null)
                Template.Creator.Recycle(Me);
            else
                Sim.DestroyEntity(Me);
            OutputTerminals[0].Fire(Payload);
        }
    }
}
